﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace BusinessManage.Common
{  
    public class CreateSign
    {
        /// <summary>
        /// 得到排序的参数、值
        /// </summary>
        /// <param name="sParaTemp">参数键值</param>
        /// <returns></returns>
        public static string getSortedData(SortedDictionary<string, string> sParaTemp)
        {
            
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            StringBuilder prestr = new StringBuilder();
            foreach (KeyValuePair<string, string> temp in sParaTemp)
            {
                //prestr.Append(temp.Key + HttpUtility.UrlEncode(temp.Value, myEncoding));
                prestr.Append(temp.Key + temp.Value);
            }
            return prestr.ToString();
        }
        
        /// <summary>
        /// 生成签名算法
        /// 一、参数排序
        ///     按key首字母排序 如遇到首字母相同 则那就看第二个字母排序,如果还相同的话就依次往下排序.
        /// 二、拼接方法 key+value
        ///     拼接字符串时候,要排除接收到的sign本身，例如  $str =“account18652929788countyId0010”；
        /// 三、计算方法
        ///     $sign = uppercase(md5(uppercase(md5($str))+$token));
        /// </summary>
        public static string CreateSignString(SortedDictionary<string, string> sParaTemp, string tokenId)
        {
            //1、参数排序：按key首字母排序
            string sortedString = getSortedData(sParaTemp);
            //2、md5加密  $sign = uppercase(md5(uppercase(md5($str))+$token));
            //string string1 = MD5.md5(sortedString,32);
            string string1 = MD5.Md5Hash(sortedString);
            string string2 = string1.ToUpper() + tokenId;
            string string3 = MD5.Md5Hash(string2);
            string signString = string3.ToUpper();
            return signString;
        }



        }
}
